package com.qipay.sp.impl;

import com.qipay.baas.model.Order;
import com.qipay.baas.model.SpChannelRule;
import com.qipay.baas.model.SpChannelRuleExample;
import com.qipay.mapper.SpChannelRuleMapper;
import com.qipay.sp.SpChannelRuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * com.qipay.sp.impl
 *
 * @author Administrator Ansen
 * @date 2017/12/27
 */
@Component
public class SpChannelRuleServiceImpl implements SpChannelRuleService{

    @Autowired
    SpChannelRuleMapper spChannelRuleMapper;

    @Transactional
    @Override
    public void setOrderSuccess(Order order) {
        SpChannelRuleExample spChannelRuleExample = new SpChannelRuleExample();
        spChannelRuleExample.setLimit(1);
        spChannelRuleExample.createCriteria().andSpIdEqualTo(order.getSpId())
                .andChannelTypeEqualTo(order.getChannelType());
        List<SpChannelRule> spChannelRules =
                spChannelRuleMapper.selectByExample(spChannelRuleExample);
        if(spChannelRules == null || spChannelRules.size() == 0) {
            return;
        }
        SpChannelRule spChannelRule = spChannelRules.get(0);
        if(spChannelRule.getCurrentTotalAmount() == 0) {
            return;
        }

        Long orderAmount = Long.valueOf(order.getAmount());
        if(spChannelRule.getCurrentTotalAmount().compareTo(orderAmount) >= 0) {
            spChannelRule.setCurrentTotalAmount(
                    spChannelRule.getCurrentTotalAmount() - orderAmount
            );
        } else {
            spChannelRule.setCurrentTotalAmount(0L);
        }
        spChannelRuleMapper.updateByPrimaryKey(spChannelRule);
    }

    @Transactional
    @Override
    public void resetCurrentTotalAmount() {
        spChannelRuleMapper.resetCurrentTotalAmount();
    }


    @Override
    public SpChannelRule selectByPrimaryKey(Long id) {
        return spChannelRuleMapper.selectByPrimaryKey(id);
    }
    @Override
    public int updateByPrimaryKeySelective(SpChannelRule spChannelRule) {
        return spChannelRuleMapper.updateByPrimaryKeySelective(spChannelRule);
    }
    @Override
    public int insert(SpChannelRule spChannelRule) {
        return spChannelRuleMapper.insert(spChannelRule);
    }

    @Override
    @Transactional
    public int delete(long id) {
        return spChannelRuleMapper.deleteByPrimaryKey(id);
    }
    @Override
    public long countByCondition(Long spId) {
        Map<String, Object> mapList = buildConditionMap(spId);
        return spChannelRuleMapper.countByCondition(mapList);
    }

    @Override
    public List<SpChannelRule> getByCondition(Long spId, int p, int pz) {
        p = p <= 0 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        Map<String, Object> mapList = buildConditionMap(spId);
        mapList.put("offset", (p - 1) * pz);
        mapList.put("size", pz);
        return spChannelRuleMapper.selectByCondition(mapList);
    }
    Map<String, Object> buildConditionMap(Long spId) {
        Map<String, Object> mapList = new HashMap<>();
        if(spId!=null){
            if (StringUtils.isNotEmpty((spId.toString()))) {
                mapList.put("spId", spId);
            }
        }
        return mapList;
    }


    @Override
    public SpChannelRule getBySpId(long spId,Byte channelType){
//        Map<String, Object> mapList = buildMap(spId,channelType);
        return  spChannelRuleMapper.selectByChannelTypeAndSpid(channelType,spId);
    }

    Map<String, Object> buildMap(Long spId,Byte channelType) {
        Map<String, Object> mapList = new HashMap<>();
        if(spId!=null){
            if (StringUtils.isNotEmpty((spId.toString()))) {
                mapList.put("spId", spId);
            }
        }if(channelType!=null){
            if (StringUtils.isNotEmpty((channelType.toString()))) {
                mapList.put("channelType", channelType);
            }
        }
        return mapList;
    }
}
